import { APP_FILTER, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core'
import { Module } from '@nestjs/common'
import { ConfigModule } from '@nestjs/config'
import { TypeOrmModule } from '@nestjs/typeorm'
import { CasbinModule } from '@ttou/nest-casbin'
import { WinstonModule } from '@ttou/nest-winston'
import { AuthModule, UserModule, UploadModule } from './modules'
import {
  WinstonOptionsService,
  TypeOrmOptionsService,
  CasbinOptionsService
} from '@/common/options'
import { HttpExceptionFilter } from '@/common/filters'
import { ValidationPipe } from '@/common/pipes'
import { LoggingInterceptor, ResponseInterceptor } from '@/common/interceptors'
import config from './config'

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      ignoreEnvFile: true,
      load: [config]
    }),
    WinstonModule.forRootAsync({
      useClass: WinstonOptionsService
    }),
    TypeOrmModule.forRootAsync({
      useClass: TypeOrmOptionsService
    }),
    CasbinModule.forRootAsync({
      useClass: CasbinOptionsService
    }),
    AuthModule,
    UserModule,
    UploadModule
  ],
  providers: [
    {
      provide: APP_PIPE,
      useClass: ValidationPipe
    },
    {
      provide: APP_FILTER,
      useClass: HttpExceptionFilter
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: LoggingInterceptor
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: ResponseInterceptor
    }
  ]
})
export class AppModule {}
